// import { login, logout, getInfo, register } from '@/api/login'
import { login, getInfo, register } from '@/api/login'
import { getToken, setToken, removeToken } from '@/utils/auth'

import router from '@/router'

const user = {
  state: {
    token: getToken(),
    name: '',
    avatar: '',
    roles: [],
    permissions: []
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_PERMISSIONS: (state, permissions) => {
      state.permissions = permissions
    }
  },

  actions: {
    // 登录
    Login({ dispatch, commit, getters, rootGetters }, userInfo) {
      const tel = userInfo.tel.trim()
      return new Promise((resolve, reject) => {
        login(tel, userInfo.password).then(response => {
          const data = response.data
          setToken(data.token)
          commit('SET_TOKEN', data.token)
          // 登录成功后，set permission
          commit('SET_PERMISSIONS', data.permissions)
          // 生成路由表
          const { permissions, supper } = data
          dispatch('GenerateRoutes', { permissions, supper }).then(() => { // 根据roles权限生成可访问的路由表
            router.addRoutes(rootGetters.addRouters) // 动态添加可访问路由表
            // router.addRoutes(.addRouters) // 动态添加可访问路由表
            // next({ ...to, replace: true }) // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record
          })
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 获取用户信息
    GetInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        getInfo(state.token).then(response => {
          const data = response.data
          if (data.permissions && data.permissions.length > 0) { // 验证返回的permissions是否是一个非空数组
            // commit('SET_ROLES', data.roles)
            // 设置permissions
            commit('SET_PERMISSIONS', data.permissions)
          } else {
            reject('getInfo: roles must be a non-null array !')
          }
          commit('SET_NAME', data.name)
          commit('SET_AVATAR', data.avatar)
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 登出
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        // logout(state.token).then(() => {
        //   commit('SET_TOKEN', '')
        //   commit('SET_ROLES', [])
        //   commit('SET_PERMISSIONS', [])
        //   removeToken()
        //   resolve()
        // }).catch(error => {
        //   console.error('logout has error')
        //   reject(error)
        // })
        commit('SET_TOKEN', '')
        commit('SET_ROLES', [])
        commit('SET_PERMISSIONS', [])
        removeToken()
        resolve()
      })
    },

    // 注册
    Register({ commit, state }, user) {
      return new Promise((resolve, reject) => {
        register(user).then((response) => {
          const data = response.data
          setToken(data.token)
          commit('SET_TOKEN', data.token)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        removeToken()
        resolve()
      })
    }
  }
}

export default user
